/**
 * 角色管理初始化
 */
var Role = {
    id: "roleTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1,
    appTree: null
};

/**
 * 初始化表格的列
 */
Role.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle', sortable: true},
        {title: '应用名称', field: 'appName', visible: true, align: 'center', valign: 'middle', sortable: true, sortName: 'appId'},
        {title: '角色名称', field: 'name', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'num', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'gmtCreate', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '更新时间', field: 'gmtModified', visible: true, align: 'center', valign: 'middle', sortable: true}
    ];
    return columns;
};

/**
 * 检查是否选中
 */
Role.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Common.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Role.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加角色
 */
Role.openAddRole = function () {
    var index = layer.open({
        type: 2,
        title: '添加角色',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Common.ctxPath + '/role/to_add'
    });
    this.layerIndex = index;
};

/**
 * 点击编辑角色
 */
Role.openEditRole = function () {
    if (this.check()) {
        var roleId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '编辑角色',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath +'/role/to_update/' + roleId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除角色
 */
Role.deleteRole = function () {
    if (this.check()) {
        var roleId = this.seItem.id;
        var operation = function(){
            var ajax = new $ax(Common.ctxPath + "/role/remove", function (result) {
                if (result.success) {
                    Common.success(result.message + "!");
                } else {
                    Common.error(result.message + "!");
                }
                Role.table.refresh();
            }, function (result) {
                Common.error(result.responseJSON.message + "!");
            });
            ajax.set("roleId", roleId);
            ajax.start();
        };
        Common.confirm("是否删除角色：" + this.seItem.appName + "-" + this.seItem.name + "?",operation);
    }
};

/**
 * 权限配置
 */
Role.setAuthority = function () {
    if (this.check()) {
        var roleId = this.seItem.id;
        var index = layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath + '/role/to_authorityAssign/' + roleId
        });
        this.layerIndex = index;
    }
};

/**
 * 重置查询
 */
Role.resetSearch = function () {
    $("#condition").val("");
    Role.search();
}

/**
 * 查询角色列表
 */
Role.search = function () {
    var queryData = {};
    queryData['appId'] = $("#appId").val();
    queryData['condition'] = $("#condition").val();
    Role.table.refresh({query: queryData});
};

/**
 * 点击应用input框时
 */
Role.onClickApp = function (e, treeId, treeNode) {
    $("#appName").val(Role.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
};

/**
 * 双击应用input框
 */
Role.onDblClickApp = function (e, treeId, treeNode) {
    $("#appName").val(Role.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
    $("#appContent").fadeOut("fast");
};

/**
 * 显示应用选择的树
 */
Role.showAppSelectTree = function () {
    var inputDiv = $("#appName");
    var inputDivOffset = inputDiv.position();
    var inputParentDivOffset = inputDiv.parent().position();
    Common.showInputTree("appName", "appContent", inputParentDivOffset.left + inputDivOffset.left, inputDiv.outerHeight());
};

$(function () {
    var defaultColunms = Role.initColumn();
    var table = new BSTable(Role.id, "/role/list", defaultColunms);
    table.setPaginationType("client");
    Role.table = table.init();
    var appTree = new $ZTree("appTree", "/tree/app");
    appTree.bindOnClick(Role.onClickApp);
    appTree.bindOnDblClick(Role.onDblClickApp)
    appTree.init();
    Role.appTree = appTree;
});
